﻿using LuckyShell.ShellValues;

namespace LuckyShell.ShellOrders;

public class ForOrder(
    ShellOrder setOrder,
    ShellOperation operation,
    ShellOrder order,
    BlockOrder blockStatement)
    : ShellOrder
{
    private ShellOrder SetOrder { get; set; } = setOrder;

    private ShellOperation Operation { get; set; } = operation;

    private ShellOrder Order { get; set; } = order;

    private BlockOrder ForBlockStatement { get; set; } = blockStatement;

    public void Run(ref VariateManager manager)
    {
        manager.AddChildren();
        SetOrder.Run(ref manager);
        while (true)
        {
            var varExpr = (bool)Operation.GetValue(ref manager);
            if (varExpr)
            {
                ForBlockStatement.Run(ref manager);
                Order.Run(ref manager);
            }
            else
            {
                manager.RemoveChildren();
                return;
            }
        }
    }
}